//
//  ZBShadowView.m
//  YouHui
//
//  Created by  on 11-12-5.
//  Copyright (c) 2011年 netease. All rights reserved.
//

#import "ZBShadowView.h"

@implementation ZBShadowView

@synthesize topColor=_topColor;
@synthesize bottomColor=_bottomColor;
@synthesize reversed=_reversed;

-(id)initWithFrame:(CGRect)frame {
    self =[super initWithFrame:frame];
    if (self) {
        _topColor = [[UIColor colorWithRed:0xd2/255.0 green:0xd2/255.0 blue:0xd2/255.0 alpha:1.0] retain];
        _bottomColor = [[UIColor colorWithRed:0xfb/255.0 green:0xfb/255.0 blue:0xfb/255.0 alpha:1.0] retain];
        self.opaque = NO;
    }
    return self;
}

-(void)dealloc {
    [_topColor release];
    [_bottomColor release];
    [super dealloc];
}

-(void)drawRect:(CGRect)rect {
    CGFloat locations[2] = { 0.0, 1.0 };
    CGFloat *colors = malloc(sizeof(CGFloat)*8);
    if (_reversed) {
        [_topColor getRed:&colors[0] green:&(colors[1]) blue:&(colors[2]) alpha:&(colors[3])]; 
        [_bottomColor getRed:&colors[4] green:&colors[5] blue:&colors[6] alpha:&colors[7]];
    } else {
        [_bottomColor getRed:&colors[0] green:&(colors[1]) blue:&(colors[2]) alpha:&(colors[3])]; 
        [_topColor getRed:&colors[4] green:&colors[5] blue:&colors[6] alpha:&colors[7]];
    }


    
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
	CGGradientRef gradient = CGGradientCreateWithColorComponents (colorspace, colors,
													  locations, 2);
	
	CGPoint startPoint = self.bounds.origin;
	CGPoint endPoint = CGPointMake(startPoint.x,startPoint.y+self.bounds.size.height);
    CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
    
    CGColorSpaceRelease(colorspace);
    CGGradientRelease(gradient);
    free(colors);
}
@end
